package com.jackchong.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.PointF
import android.graphics.Rect
import android.os.Looper
import android.os.SystemClock
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider
import com.uber.autodispose.autoDisposable
import io.reactivex.Observable
import io.reactivex.ObservableSource
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.functions.BiFunction
import io.reactivex.schedulers.Schedulers
import io.reactivex.functions.Function
import java.util.concurrent.TimeUnit

/**
 * author : chong.huang
* create : 1/24/21
 * desc   :
 */
object RxJavaTest {

    lateinit var activity: AppCompatActivity


    fun test(activity: AppCompatActivity){
        this.activity = activity

//        common()

//        zip()

//        doOnNext()

        interval()
    }

    private fun common() {

        val disposable = Observable.create<String> {

            it.onNext("1")
            SystemClock.sleep(1000)
            it.onNext("2")
            SystemClock.sleep(1000)
            it.onNext("3")
            SystemClock.sleep(1000)
            it.onComplete()

            Observable.error<RuntimeException> { RuntimeException("error") }
            it.onError(RuntimeException("error"))

        }.
            subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .autoDisposable(AndroidLifecycleScopeProvider.from(activity.lifecycle, Lifecycle.Event.ON_DESTROY))
                .subscribe ({
                    Log.i("qqq", it)
                }, {
                    Log.i("qqq", it.message!!)
                }, {
                    Log.i("qqq", "onComplete")
                })
    }


    private fun zip() {

        val create1 = Observable.create<String> {
            it.onNext("1")
        }
        val create2 = Observable.create<PointF> {
            it.onNext(PointF(100F, 100F))
        }
        val create3 = Observable.create<Rect> {
            SystemClock.sleep(1000)
            it.onNext(Rect(0, 0, 1080, 1920))
        }


        //zip, 合并
        Observable.zip(create2, create3,
            BiFunction<PointF, Rect, String> { t1, t2 -> t1.toString() + t2.toString() })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .autoDisposable(AndroidLifecycleScopeProvider.from(activity.lifecycle, Lifecycle.Event.ON_DESTROY))
            .subscribe {
                Log.i("qqq", it + "    " + Looper.getMainLooper().thread.id + "  " +Thread.currentThread().id.toString())
            }

    }

    private fun doOnNext() {

        Observable.create<String> {
            it.onNext("1")
        }.observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .doOnNext {
                Log.i("qqq", "doOnNext" + it)
            }.flatMap { Observable.just(5) }
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .autoDisposable(AndroidLifecycleScopeProvider.from(activity.lifecycle, Lifecycle.Event.ON_DESTROY))
            .subscribe {
                Log.i("qqq", "doOnNext" + it)
            }
    }

    private fun interval() {
        var disposable: Disposable? = null
        disposable = Observable.interval(1000, TimeUnit.MILLISECONDS)
            .doOnNext {
                if (it > 5){
                    disposable?.dispose()
                }
                Log.i("qqq", "interval" + it)
            }
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .autoDisposable(AndroidLifecycleScopeProvider.from(activity.lifecycle, Lifecycle.Event.ON_DESTROY))
            .subscribe {

                Log.i("qqq", "interval" + it)
            }
    }


}